#include "stdafx.h"
#include "MenuState.h"
#include "ResourceManager.h"
#include "Marco.h"
#include "Globals.h"
#include "StateManager.h"
#include "Picture.h"
#include "GamePlay.h"
#include "TextRenderer.h"
#include "Terrain.h"
#include "LookAtCamera.h"
#include "Game.h"

MenuState::MenuState() : State()
{
	m_NumEntities += 2;
	m_ListEntities = new Entity*[2];
	/*m_ListEntities[0] = new Tank("../Resources/TankModel/T-90/T-90.txt");*/
	int err = 0;
	int texCount = 5;
	char **aszFile = new char*[texCount];
	aszFile[0] = "../Resources/Textures/Blend.tga";
	aszFile[1] = "../Resources/Textures/Grass.tga";
	aszFile[2] = "../Resources/Textures/Dirt.tga";
	aszFile[3] = "../Resources/Textures/Rock.tga";
	aszFile[4] = "../Resources/Textures/Water.tga";

	Globals::terrain = new Terrain("../Resources/Textures/heightMap.raw", aszFile, texCount);
	Globals::terrain->Init();
	Globals::terrain->setShader(ResourceManager::GetInstance()->terrainShader);
	Globals::terrain->setProperties(1, Vector3(10.0f, 2.0f, 10.0f));
	//terrain->SetScaleFactor(Vector3(20.0f, 4.0f, 20.0f));
	SAFE_DEL_ARRAY(aszFile);
	this->AddEntity(Globals::terrain);

	Tank* newTank = new Tank("../Resources/TankModel/T-90/T-90.txt");
	Vector3 pos = Vector3(0.0f, 300.0f, 0.0f);
	newTank->target = Vector3(0.0f, 300.0f, -10.0f);
	newTank->gunPositon = Vector3(pos.x, pos.y+10, pos.z-0);
	newTank->targetShot = Vector3(pos.x, pos.y+10, pos.z - 70);
	newTank->SetPostion(pos);
	newTank->SetScale(Vector3(0.1f, 0.1f, 0.1f));
	//m_ListEntities[0] = new Tank("../Resources/TankModel/T-90/T-90.txt");*/
	this->AddEntity(newTank);

	delete Globals::currentCamera;
	Globals::currentCamera = new LookAtCamera();
	Globals::currentCamera->projection.SetPerspective(45.0f, 4.0f / 3, 1.0f, 10000.0f);
	Globals::currentCamera->SetPosition(Vector3(pos.x - 10, pos.y + 100, pos.z + 100));
	LookAtCamera* temp = dynamic_cast<LookAtCamera*>(Globals::currentCamera);
	temp->SetTarget(Vector3(pos.x, pos.y + 30, pos.z));
	/*LookAtCamera* temp = dynamic_cast<LookAtCamera*>(Globals::currentCamera);
	
	Globals::currentCamera->SetPosition(newTank->gunPositon);*/
	//temp->SetTarget(Vector3(newTank->target.x, newTank->target.y+0, newTank->target.z-100));


	//this->AddEntity(m_ListEntities[1]);
	//start = new Button2D();
	//start->setProperties(1, Vector3(4.0f, 2.0f, 4.0f));
	//start->G(ResourceManager::GetInstance()->buttonBackGround,0);
	////start->SetShader(ResourceManager::GetInstance()->textureShader);
	//end = new Button2D();
	//end->SetScaleFactor(Vector3(4.0f, 2.0f, 4.0f));
	//end->SetTexture(ResourceManager::GetInstance()->buttonBackGround, 0);
	//start->SetPositon(Vector3(0.0f, 0.0f, -30.0f));
	//Vector3 pos = start->GetPosition();
	//Vector3 newPos = Vector3(pos.x, pos.y - 3.0f, pos.z);
	//end->SetPositon(newPos);

	////Button2D* ed = new Button2D();
	///*
	//pos = end->GetPosition();
	//newPos = Vector3(pos.x, pos.y - 3.0f, pos.z);
	//ed->SetPositon(newPos);*/
	//pic = new Picture();
	//////pic->SetBackGround("..\\Resources\\Textures\\Rock.tga");
	//pic->SetTexture(ResourceManager::GetInstance()->menuBackGround, 0);
	//pic->SetSize(Globals::screenWidth, Globals::screenHeight);
	//pic->SetShader(ResourceManager::GetInstance()->triangleShader);
	//pic->SetPositon(Vector3(-Globals::screenWidth / 2, -Globals::screenHeight / 2, -640.0f));
	//this->AddEntity(pic);
	//this->AddEntity(start);
	//this->AddEntity(end);
	////this->AddEntity(ed);

	////TextRenderer* instance=TextRenderer::GetInstance();
}


int MenuState::Init()
{ 

	return 0;
}

MenuState::~MenuState()
{
	//Delete(start);
	//Delete(end);
	//Delete(pic);
	//TextRenderer * txt = TextRenderer::GetInstance();
	//Delete(txt);
}

void MenuState::Key(ESContext *esContext, unsigned char key, bool bIsPressed)
{
	LookAtCamera* temp;
	Vector3 pos;
	switch (key){
	case 32:
		StateManager::GetInstance()->currentState = StateManager::GetInstance()->GetState(1);
		break;
	case 'W':
		//Globals::currentCamera->MoveForward(10.0f);
		//m_ListEntities[1]->SetPostion(Vector3(100, 200, 0));
		((Tank*)m_ListEntities[1])->MoveForward(10.0f);
		Globals::currentCamera->SetPosition(((Tank*)m_ListEntities[1])->gunPositon);
		/*temp = dynamic_cast<LookAtCamera*>(Globals::currentCamera);
		if (temp)
		{
			((Tank*)m_ListEntities[1])->MoveForward(10.0f);
			pos = m_ListEntities[1]->getProperties(0);
			temp->SetTarget(Vector3(pos.x, pos.y+30, pos.z));
			Globals::currentCamera->SetPosition(Vector3(pos.x - 10, pos.y + 100, pos.z + 100));
		}
		else
		Globals::currentCamera->MoveForward(10.0f);*/
		break;
		
	case 'S':
		temp = dynamic_cast<LookAtCamera*>(Globals::currentCamera);
		if (temp)
		{
			((Tank*)m_ListEntities[1])->MoveBackward(10.0f);
			pos = m_ListEntities[1]->getProperties(0);
			temp->SetTarget(Vector3(pos.x, pos.y+30, pos.z));
			Globals::currentCamera->SetPosition(Vector3(pos.x - 10, pos.y + 100, pos.z + 100));
		}
		else
			Globals::currentCamera->MoveBackward(10.0f);
		break;
	case 'A':
		Globals::currentCamera->MoveLeft(2);
		//((Tank*)m_ListEntities[1])->isMoveLeft = true;
		break;
	case 'D':
		Globals::currentCamera->MoveRight(2);
		break;
	case 38:
		Globals::currentCamera->RotateUp(3.14f / 90);
		break;
	case 40:
		Globals::currentCamera->RotateDown(3.14f / 90);
		break;
	case 39:
		Globals::currentCamera->RotateRight(0.05f);
		break;
	case 'L':
		((Tank*)m_ListEntities[1])->RotateRight(0.05f);
		Globals::currentCamera->SetPosition(((Tank*)m_ListEntities[1])->gunPositon);
		Globals::currentCamera->RotateRight(0.05f);
		
		//Globals::currentCamera->MoveForward(60.0f);
		break;
	case 'K':
		((Tank*)m_ListEntities[1])->RotateLeft(0.05f);
		Globals::currentCamera->SetPosition(((Tank*)m_ListEntities[1])->gunPositon);
		Globals::currentCamera->RotateLeft(0.05f);
		//Globals::currentCamera->MoveForward(60.0f);
		break;
	case 37:
		Globals::currentCamera->RotateLeft(0.05f);
		//((Tank*)m_ListEntities[1])->RotateLeft(0.05f);
		break;
	case 'M':
		GamePlay *gamePlay = new GamePlay();
		gamePlay->Init();
		StateManager::GetInstance()->AddState(gamePlay);
		StateManager::GetInstance()->SwitchState(StateManager::GetInstance()->GetState(1),false);
	}
}

void MenuState::Mouse(ESContext* esContext, MouseData mouseData, bool bIsdown)
{
	if (mouseData.type == MouseType::Left)
	{
		if (bIsdown)
		{
			((Tank*)m_ListEntities[1])->isShooting = true;
			//LookAtCamera* temp = dynamic_cast<LookAtCamera*>(Globals::currentCamera);
			////temp->SwicthToShotView(m_ListEntities[1]->getProperties(0), ((Tank*)m_ListEntities[1])->rotateAngleGun);
			//temp->SwicthToShotView(m_ListEntities[1]->getProperties(0), ((Tank*)m_ListEntities[1])->gunPositon, ((Tank*)m_ListEntities[1])->rotateAngleGun);
			LookAtCamera* temp = dynamic_cast<LookAtCamera*>(Globals::currentCamera);
			temp->SwicthToShotView(m_ListEntities[1]->getProperties(0), ((Tank*)m_ListEntities[1])->gunPositon, ((Tank*)m_ListEntities[1])->targetShot);
		}
	}
	if (mouseData.type == MouseType::Right)
	{
		if (bIsdown)
		{
			((Tank*)m_ListEntities[1])->isShooting = false;
			Vector3 pos = m_ListEntities[1]->getProperties(0);
			LookAtCamera* temp = dynamic_cast<LookAtCamera*>(Globals::currentCamera);
			temp->SetTarget(Vector3(pos.x, pos.y + 30, pos.z));
			Globals::currentCamera->SetPosition(Vector3(pos.x - 10, pos.y + 100, pos.z + 100));
		}
	}
}

void MenuState::Update(ESContext* esContext, float deltatime)
{
	for (int i = 0; i < m_NumEntities; i++)
		m_ListEntities[i]->update(deltatime);
	if (((Tank*)m_ListEntities[1])->isMoveLeft)
	{
		((Tank*)m_ListEntities[1])->MoveLeft(10.0f);
		Vector3 pos = m_ListEntities[1]->getProperties(0);
		LookAtCamera* temp = dynamic_cast<LookAtCamera*>(Globals::currentCamera);
		temp->SetTarget(Vector3(pos.x, pos.y + 30, pos.z));
		Globals::currentCamera->SetPosition(Vector3(pos.x - 10, pos.y + 100, pos.z + 100));
	}
	/*if (((Tank*)m_ListEntities[1])->isShooting)
	{
		LookAtCamera* temp = dynamic_cast<LookAtCamera*>(Globals::currentCamera);
		temp->SwicthToShotView(m_ListEntities[1]->getProperties(0), ((Tank*)m_ListEntities[1])->gunPositon, ((Tank*)m_ListEntities[1])->targetShot);
	}*/

	//start->Update(esContext, deltatime);
}

void MenuState::Draw(ESContext* esContext)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	//glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
	glEnable(GL_DEPTH_TEST);

	for (int i = 0; i < m_NumEntities; i++)
		m_ListEntities[i]->draw();
	/*start->Draw(ResourceManager::GetInstance()->textureShader, esContext);
	end->Draw(ResourceManager::GetInstance()->textureShader, esContext);*/
	/*start->Draw(esContext);
	end->Draw(esContext);*/
	//pic->Draw(esContext);
	/*State::Draw(esContext);*/
	/*TextRenderer* instance = TextRenderer::GetInstance();
	float sx = 2.0 / Globals::screenWidth;
	float sy = 2.0 / Globals::screenHeight;
	instance->RenderText(esContext, "abc", -1 + 8 * sx, 1 - 50 * sy, sx, sy);*/
}



int MenuState::Release()
{ 
	return 0;
}